package com.anzia.study_9.slice;

import com.anzia.study_9.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.data.DatabaseFileType;
import ohos.data.DatabaseHelper;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;

public class MainAbilitySlice extends AbilitySlice {

	private Button openButton, createButton;

	private static final RdbOpenCallback callback = new RdbOpenCallback() {
		@Override
		public void onCreate(RdbStore rdbStore) {
			// 数据库创建是被回调，开发者可以在该方法中初始化表结构，并添加一些应用应道的初始化数据
			rdbStore.executeSql("create table if not exists test(id integer primary key autoincrement, name text not null, age integer, salary real)");
		}

		@Override
		public void onUpgrade(RdbStore rdbStore, int i, int i1) {

		}
	};

	private static final RdbOpenCallback open_callback = new RdbOpenCallback() {
		@Override
		public void onCreate(RdbStore rdbStore) {

		}

		@Override
		public void onUpgrade(RdbStore rdbStore, int i, int i1) {

		}
	};

	@Override
	public void onStart(Intent intent) {
		super.onStart(intent);
		super.setUIContent(ResourceTable.Layout_ability_main);

		createButton = (Button) findComponentById(ResourceTable.Id_create_db_button);
		createButton.setClickedListener(component -> {
			// 创建数据库
			StoreConfig.Builder builder = new StoreConfig.Builder();
			String keystr = "123456";
			builder.setName("tttt.db")
					.setSyncMode(null)
					.setJournalMode(StoreConfig.JournalMode.MODE_WAL)
					.setStorageMode(StoreConfig.StorageMode.MODE_DISK)
					.setReadOnly(false)
					.setDatabaseFileType(DatabaseFileType.NORMAL)
					.setEncryptKey(keystr.getBytes());
			StoreConfig config = builder.build();
			// 进入创建数据库的实质环节，重要的核心类
			DatabaseHelper helper = new DatabaseHelper(MainAbilitySlice.this);
			// 进一步通过helper的方法，来获取到数据库对象，getRdbStore：如果数据库不存在，自动创建
			// config:配置信息；1:版本信息；callback:回调；null:钩子
			RdbStore rdbStore = helper.getRdbStore(config, 1, callback, null);

			// 插入数据到test表
			ValuesBucket valuesBucket = new ValuesBucket();
			valuesBucket.putInteger("id", 1);
			valuesBucket.putString("name", "anzia");
			valuesBucket.putInteger("age", 18);
			valuesBucket.putDouble("salary", 8000.5);
			long rowid1 = rdbStore.insert("test", valuesBucket);
			System.out.println("====rowid1" + rowid1);

			valuesBucket.putInteger("id", 2);
			valuesBucket.putString("name", "zhangsan");
			valuesBucket.putInteger("age", 28);
			valuesBucket.putDouble("salary", 18000.5);
			long rowid2= rdbStore.insert("test", valuesBucket);
			System.out.println("====rowid2" + rowid1);
		});

		openButton = (Button) findComponentById(ResourceTable.Id_open_db_button);
		openButton.setClickedListener(component -> {
			// 打开已存在的数据库
			StoreConfig.Builder builder = new StoreConfig.Builder();
			String keystr = "123456";
			builder.setName("tttt.db")
					.setSyncMode(null)
					.setJournalMode(StoreConfig.JournalMode.MODE_WAL)
					.setStorageMode(StoreConfig.StorageMode.MODE_DISK)
					.setReadOnly(false)
					.setDatabaseFileType(DatabaseFileType.NORMAL)
					.setEncryptKey(keystr.getBytes());
			StoreConfig config = builder.build();
			// 进入创建数据库的实质环节，重要的核心类
			DatabaseHelper helper = new DatabaseHelper(MainAbilitySlice.this);
			// 进一步通过helper的方法，来获取到数据库对象，getRdbStore：如果数据库不存在，自动创建
			// config:配置信息；1:版本信息；callback:回调；null:钩子
			RdbStore rdbStore = helper.getRdbStore(config, 1, open_callback, null);

			// 原生sql方式
			ResultSet resultSet = rdbStore.querySql("select id,name,age,salary from test where age=? order by salary desc", new String[]{"28"});

			for (int i = 0; i < resultSet.getRowCount(); i++) {
				resultSet.goToRow(i);
				System.out.println("====id：" + resultSet.getInt(resultSet.getColumnIndexForName("id")));
				System.out.println("====name：" + resultSet.getString(resultSet.getColumnIndexForName("name")));
				System.out.println("====age：" + resultSet.getInt(resultSet.getColumnIndexForName("age")));
				System.out.println("====salary：" + resultSet.getDouble(resultSet.getColumnIndexForName("salary")));
			}
		});

		// 创建数据库，前提：先要准备数据库配置信息，简单起见只配置数据库名字，其他都为默认值
		// StoreConfig config = StoreConfig.newDefaultConfig("RdbStoreTest.db");

		// System.out.println(config.getName());
		// System.out.println(config.isReadOnly());
		// System.out.println(config.getDatabaseFileType());
		// System.out.println(config.getJournalMode());
		// System.out.println(config.getStorageMode());
		// System.out.println(config.getSyncMode());
		// System.out.println(config.getEncryptKey());




		;

		// 谓词方式查询
		// RdbPredicates rdbPredicates = new RdbPredicates("test").equalTo("age", 28).orderByDesc("salary");
		// String[] columns = {"id", "name", "age", "salary"};
		// ResultSet resultSet = rdbStore.query(rdbPredicates, columns);

		// 原生sql方式
		// ResultSet resultSet = rdbStore.querySql("select id,name,age,salary from test where age=? order by salary desc", new String[]{"28"});
		//
		// for (int i = 0; i < resultSet.getRowCount(); i++) {
		// 	resultSet.goToRow(i);
		// 	System.out.println("====id：" + resultSet.getInt(resultSet.getColumnIndexForName("id")));
		// 	System.out.println("====name：" + resultSet.getString(resultSet.getColumnIndexForName("name")));
		// 	System.out.println("====age：" + resultSet.getInt(resultSet.getColumnIndexForName("age")));
		// 	System.out.println("====salary：" + resultSet.getDouble(resultSet.getColumnIndexForName("salary")));
		// }

		// 实现对数据记录的更新
		// 用谓词
		// ValuesBucket replaceVB = new ValuesBucket();
		// replaceVB.putString("name", "anzia1");
		//
		// RdbPredicates replace_rdbPredicates = new RdbPredicates("test").equalTo("id", 1);
		//
		// int update = rdbStore.update(replaceVB, replace_rdbPredicates);

		// 用原生的sql来做更新
		// rdbStore.executeSql("update test set name = 'anzia2' where id = ?", new Object[] {1});

		// ResultSet resultSet = rdbStore.querySql("select id,name,age,salary from test", new String[] {});
		//
		// for (int i = 0; i < resultSet.getRowCount(); i++) {
		// 	resultSet.goToRow(i);
		// 	System.out.println("====id：" + resultSet.getInt(resultSet.getColumnIndexForName("id")));
		// 	System.out.println("====name：" + resultSet.getString(resultSet.getColumnIndexForName("name")));
		// 	System.out.println("====age：" + resultSet.getInt(resultSet.getColumnIndexForName("age")));
		// 	System.out.println("====salary：" + resultSet.getDouble(resultSet.getColumnIndexForName("salary")));
		// }

		// 删除数据
		// rdbStore.delete(new RdbPredicates("test").equalTo("id", 2));

		// 原生sql
		// rdbStore.executeSql("delete from test where id=?", new Object[] {2});
		//
		// ResultSet resultSet1 = rdbStore.querySql("select id,name,age,salary from test", new String[] {});
		//
		// for (int i = 0; i < resultSet1.getRowCount(); i++) {
		// 	resultSet.goToRow(i);
		// 	System.out.println("====id：" + resultSet.getInt(resultSet.getColumnIndexForName("id")));
		// 	System.out.println("====name：" + resultSet.getString(resultSet.getColumnIndexForName("name")));
		// 	System.out.println("====age：" + resultSet.getInt(resultSet.getColumnIndexForName("age")));
		// 	System.out.println("====salary：" + resultSet.getDouble(resultSet.getColumnIndexForName("salary")));
		// }

	}

	@Override
	public void onActive() {
		super.onActive();
	}

	@Override
	public void onForeground(Intent intent) {
		super.onForeground(intent);
	}
}
